﻿namespace Yom.WebService.Setup
{
    using System;
    using System.IO;
    using System.Text;
    using System.Web;
    using Yom.Common;

    public class SetupCheckRun
    {
        public void OutWrite()
        {
            HttpResponse Response = HttpContext.Current.Response;
            HttpServerUtility Server = HttpContext.Current.Server;
            StringBuilder strInfo = new StringBuilder("开始检测创建目录权限.....");
            int State = 0;

            try
            {
                Directory.CreateDirectory(Server.MapPath("~/Yom"));
                strInfo.Append("<font color=blue>创建目录Yom成功！</font>");
            }
            catch (Exception ex)
            {
                strInfo.Append("<font color=red>创建目录失败！</font>,错误信息：" + ex.Message.ToString() + ", 请检查网站权限是否设置正确!");
                State = 1;
            }

            strInfo.Append("<br />开始检测创建文件权限.....");

            try
            {
                StreamWriter sw = new StreamWriter(Server.MapPath("~/Yom/Yom.txt"), false, Encoding.UTF8);
                sw.Write("YomComic 创建测试文件");
                sw.Close();
                strInfo.Append("<font color=blue>创建文件Yom/Yom.txt成功！</font>");
            }
            catch (Exception ex)
            {
                strInfo.Append("<font color=red>创建文件失败！</font>,错误信息：" + ex.Message.ToString() + ", 请检查网站权限是否设置正确!");
                State = 1;
            }

            strInfo.Append("<br />开始检测删除文件权限.....");

            try
            {
                FileInfo stm = new FileInfo(Server.MapPath("~/Yom/Yom.txt"));
                stm.Delete();
                strInfo.Append("<font color=blue>删除文件Yom/Yom.txt成功！</font>");
            }
            catch (Exception ex)
            {
                strInfo.Append("<font color=red>删除文件失败！</font>,错误信息：" + ex.Message.ToString() + ", 请检查网站权限是否设置正确!");
                State = 1;
            }

            strInfo.Append("<br />开始检测删除目录权限.....");

            try
            {
                DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Yom"));
                dir.Delete(true);
                strInfo.Append("<font color=blue>删除目录Yom成功！</font>");
            }
            catch (Exception ex)
            {
                strInfo.Append("<font color=red>删除目录失败！</font>,错误信息：" + ex.Message.ToString() + ", 请检查网站权限是否设置正确!");
                State = 1;
            }

            strInfo.Append("<br /><br />测试完毕！");

            JsonModel Js = new JsonModel();
            Js.Add("Title", "<p>检查运行环境</p>");
            Js.Add("Desc", "");
            Js.Add("Info", strInfo.ToString());
            Js.Add("Height", "30px");
            Js.Add("Button", "<input type=\"button\" id=\"Previous\" value=\"&lt; 上一步\" />&nbsp;<input type=\"button\" id=\"Next\" value=\"下一步 &gt;\" disabled=\"disabled\" />");
            Js.Add("State", State);
            Response.Write(new Json(Js));
        }
    }
}